package com.example.zyfyptlzx729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.activities.ViewArticleActivity;
import com.example.zyfyptlzx729.activities.ViewVideoActivity;
import com.example.zyfyptlzx729.bean.VideonewBean;
import com.example.zyfyptlzx729.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class VideonewAdapter extends RecyclerView.Adapter {
    private List<VideonewBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    public VideonewAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tvtitle, tvauthor, tvtime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView2);
            tvtitle = itemView.findViewById(R.id.textView4);
            tvauthor = itemView.findViewById(R.id.textView6);
            tvtime = itemView.findViewById(R.id.textView10);
        }
    }
    public void setList(List<VideonewBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final VideonewBean bean=list.get(position);
        if(bean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        Picasso.with(context)
                .load(Common.IMAGEURL+bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.tvtitle.setText(bean.getName());
        viewHolder.tvauthor.setText(bean.getAuthor());
        viewHolder.tvtime.setText(bean.getUpdate_time());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=bean.getId();
                Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(context, ViewVideoActivity.class);
                intent.putExtra("videopath", bean.getVideopath());
                intent.putExtra("authorid",bean.getUserid());
                context.startActivity(intent);
//
//                Intent intent1=new Intent(context, ViewArticleActivity.class);
//                intent1.putExtra("tcase",bean.getId());
//                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
}
